home *** CD-ROM | disk | FTP | other *** search
- /*
- * ha.c,v 1.1 1996/05/27 16:04:38 miksic Exp
- */
-
- /* HA.C -- Example LONWORKS Host Application.
- * Copyright (c) 1993 by Echelon Corporation.
- * All Rights Reserved.
- *
- * Main program for an example LONWORKS host application. The example
- * sends and receives both polls and updates to network variables. The
- * host application node declares and implements eight network variables.
- * The example also processes incoming network management commands for
- * network variable binding so that network variables may be bound by a
- * network management tool.
- *
- * The example uses a LONWORKS standard network driver to communicate with
- * a LONWORKS network interface such as the Echelon Serial LONTALK Adapter
- * (SLTA) or a Microprocessor Interface Program (MIP)-based network interface.
- *
- * The example implements an application node that will be installed by
- * another network management tool.
- */
-
- /****************************** Include Files *******************************/
-
- #include "ni_msg.h" /* Network interface data structures */
- #include "ni_mgmt.h" /* Network management data structures */
- #include "hauif.h" /* User interface prototypes */
- #include <stdio.h> /* For printf() */
- #include <stdlib.h> /* for random() */
- #include <time.h> /* for time( ) */
- #include "conio.h" /* for kbhit( ) */
- #include <ctype.h> /* For toupper() */
- char * lon_device_name = "/dev/pclta";
-
- /*************************** Function Prototypes ****************************/
-
-
- extern boolean process_msg( ServiceType, RcvAddrDtl *,
- MsgData * in_data, int in_length, boolean in_auth ); // in APPLMSG.C
-
- extern void process_cmd( void ); /* Process keyboard command */
- extern void error_exit( void );
- extern void sign_on( void ); // in HAUIF.C
- extern void load_NV_config( void ); // in APPLMSG.C
- void install_prog_ID( void ); // in HA.C
-
- /*
- ****************************************************************************
- * main(). Display sign-on message, open network driver, and enter
- * the main scheduler loop.
- ****************************************************************************
- */
-
- int main(int argc, char *argv[])
- {
- NI_Code ni_error;
- ServiceType service; /* ACKD, UNACKD_RPT, UNACKD, REQUEST */
- RcvAddrDtl in_addr; /* address of incoming msg */
- boolean in_auth; /* if incoming was authenticated */
- MsgData in_data; /* data of incoming msg */
- int in_length; /* length of incoming msg */
- boolean prompt_flag;
- const boolean true = TRUE; /* Avoid compiler warning */
- const char * prompt = "HA> ";
-
- /* for terminal */
- get_normal_terms();
- atexit( go_normal_terms );
-
- /* unbuffered output */
- setvbuf( stdout, NULL, _IONBF, 0 );
-
- /* Process command line arguments. */
- while( --argc ) {
- if( ( argv[ argc ][ 0 ] == '/' ) || (argv [ argc ][ 0 ] == '-') ) {
- switch( toupper( argv[ argc ][ 1 ] ) ) {
- case 'D':
- /* Device name */
- if( argv[ argc ][ 2 ] ) {
- /* Device name specified, copy it */
- lon_device_name = &argv[ argc ][ 2 ];
- } else error_exit( );
- break;
-
- case 'V':
- /* Set verbose mode */
- verbose_flag = TRUE;
- break;
-
- default:
- /* Invalid flag */
- error_exit( );
- }
- } else error_exit( );
- }
-
- sign_on( ); // print sign on message
-
- /* Open and initialize the network interface. */
- if( ( ni_error = ni_init( lon_device_name ) ) != NI_OK ) {
- ni_error_display( "Error while initializing network interface",
- ni_error );
- return( 1 );
- }
-
- load_NV_config( ); // load network variable config table from disk
- install_prog_ID( ); // install program ID into network interface
- printf( prompt );
- while( true ) { // main processing loop
-
- if( kbhit( ) ) { // Check for keyboard activity
- process_cmd( ); /* handle keyboard input */
- printf( prompt );
- }
-
- ni_error = ni_receive_msg( // Check for network activity
- & service, /* handle incoming message to this node */
- & in_addr,
- & in_data,
- & in_length,
- & in_auth );
-
- prompt_flag = FALSE;
- if( ni_error == NI_UPLINK_CMD ) prompt_flag = TRUE;
- else if( ni_error == NI_OK )
- prompt_flag = process_msg( service, & in_addr,
- & in_data, in_length, in_auth );
-
- if( prompt_flag ) printf( prompt );
-
- } /* never leave this loop */
- return 0;
- }
-
- /*
- ****************************************************************************
- * NV_table() - display node's NV config table
- ****************************************************************************
- */
-
- void NV_table( void ) {
-
- extern const char * svc_table[ ]; // strings for service type
- extern const int num_nvs; // in APPLMSG.C
- extern nv_struct nv_config_table[ ]; // network variable config table
- extern network_variable nv_value_table[ ]; // NV value table
-
- nv_struct * nv_config_ptr;
- network_variable * nv_value_ptr;
- int nv_index;
-
- printf( "Index\tSelctr\tDir\tPrio\tAuth\tAddridx\tService\tTrnArnd\n" );
-
- nv_config_ptr = nv_config_table;
- nv_value_ptr = nv_value_table;
-
- for( nv_index = 0; nv_index < num_nvs; nv_index++ ) {
-
- printf( "%d\t", nv_index );
- printf( "%2.2x%2.2x\t", nv_config_ptr->selector_hi,
- nv_config_ptr->selector_lo );
- printf( nv_config_ptr->direction ? "out\t" : "in\t" );
- printf( nv_config_ptr->priority ? "yes\t" : "no\t" );
- printf( nv_config_ptr->auth ? "yes\t" : "no\t" );
- printf( "%d\t", nv_config_ptr->addr_index );
- printf( "%s\t", svc_table[ nv_config_ptr->service ] );
- printf( nv_config_ptr->turnaround ? "yes " : "no " );
- printf( "%s\n", nv_value_ptr->name );
-
- nv_config_ptr++; // go to next
- nv_value_ptr++;
- }
- }
-
- /*
- ****************************************************************************
- * install_prog_ID - install a program ID into the network interface
- ****************************************************************************
- */
-
- void install_prog_ID( void ) {
- struct {
- NM_write_memory_request write_mem_hdr;
- char prog_ID[ ID_STR_LEN ]; // data to be written
- } write_mem_msg = {
- { NM_write_memory, // code
- READ_ONLY_RELATIVE, // mode
- 0x00, 0x0D, // offset for id_string
- ID_STR_LEN, // count
- BOTH_CS_RECALC }, // form
- "HostAppl" }; // program ID
-
- ComplType completion;
- MsgData response;
- NI_Code ni_error;
- SendAddrDtl net_intfc_addr;
-
- net_intfc_addr.lc.type = LOCAL; // send it to the network intfc
- ni_error = ni_send_msg_wait(
- REQUEST,
- & net_intfc_addr, // out_addr
- ( MsgData * ) &write_mem_msg, // out_data
- sizeof( write_mem_msg ), // out_length
- FALSE, // priority
- FALSE, // out_auth
- & completion, // completion code
- NULL, // num_responses
- NULL, // in_addr
- & response, // in_data
- NULL ); // in_length
-
- ( void ) handle_error( ni_error, completion, response.exp.code,
- "updating program ID" ); // report any errors
- }
-